﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.Snowflake;
using ZilLion.Service.WeChatMall.Domain.Entitys.AppInfo;
using ZilLion.Service.WeChatMall.Domain.Entitys.ShopInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.AppInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.ShopInfo;
using ZilLion.Service.WeChatMall.Domain.Services.Interface;

namespace ZilLion.Service.WeChatMall.Domain.Services
{
    public class ShopInfoService : IShopInfoService
    {
        private readonly IAppShopRelationShipReponsitory _appShopRelationShipReponsitory;
        private readonly IOfflineShopRelationshipReponsitory _offlineShopRelationshipReponsitory;
        private readonly IOfflineShopReponsitory _offlineShopReponsitory;
        private readonly IShopBasicInfoRepository _shopBasicInfoRepository;
        private readonly IShopExtraInfoReponsitory _shopExtraInfoReponsitory;
        private readonly IShopGsRelationShipReponsitory _shopGsRelationShipReponsitory;
        private readonly IShopSupportsInfoReponsitory _shopSupportsInfoReponsitory;

        public ShopInfoService(IShopExtraInfoReponsitory shopExtraInfoReponsitory,
            IShopBasicInfoRepository shopBasicInfoRepository, IShopSupportsInfoReponsitory shopSupportsInfoReponsitory,
            IAppShopRelationShipReponsitory appShopRelationShipReponsitory,
            IShopGsRelationShipReponsitory shopGsRelationShipReponsitory,
            IOfflineShopRelationshipReponsitory offlineShopRelationshipReponsitory,
            IOfflineShopReponsitory offlineShopReponsitory)
        {
            _shopExtraInfoReponsitory = shopExtraInfoReponsitory;
            _shopBasicInfoRepository = shopBasicInfoRepository;
            _shopSupportsInfoReponsitory = shopSupportsInfoReponsitory;
            _appShopRelationShipReponsitory = appShopRelationShipReponsitory;
            _shopGsRelationShipReponsitory = shopGsRelationShipReponsitory;
            _offlineShopRelationshipReponsitory = offlineShopRelationshipReponsitory;
            _offlineShopReponsitory = offlineShopReponsitory;
        }

        public IList<ShopInfoDto> GetShopsByAppid(string appid)
        {
            var result = new List<ShopInfoDto>();
            var basics = _shopBasicInfoRepository.GetShopByAppId(appid);
            var extraInfos = _shopExtraInfoReponsitory.GetShopExtraInfoByappId(appid).ToList();
            var supportsInfos = _shopSupportsInfoReponsitory.GetShopSupportsInfoByAppId(appid).ToList();


            foreach (var shop in basics)
            {
                var supports = supportsInfos.Where(x => x.ShopId == shop.ShopId).ToList();
                var extraInfo = extraInfos.FirstOrDefault(x => x.ShopId == shop.ShopId);
                var dto = ShopInfoDto.Convert(shop, extraInfo, supports, appid);
                result.Add(dto);
            }
            return result;
        }

        public ShopInfoDto GetShopById(long shopid)
        {
            var app = _appShopRelationShipReponsitory.GetRelationShipByShopid(shopid);
            var supports = _shopBasicInfoRepository.GetShopByid(shopid);
            var extraInfo = _shopExtraInfoReponsitory.GetShopExtraInfoById(shopid);
            var supportsInfos = _shopSupportsInfoReponsitory.GetShopSupportsInfoById(shopid).ToList();
            var dto = ShopInfoDto.Convert(supports, extraInfo, supportsInfos, app.AppId);
            return dto;
        }

        public ShopInfoDto SaveShopInfo(ShopBasicInfoEntity basic, AppShopRelationShipEntity appShopRelationShip = null,
            IList<ShopSupportInfoEntity> shopSupports = null)
        {
            if (basic.ShopId == 0)
                basic.ShopId = SnowflakeIdCreater.Instance().GetNextId();
            using (var unit = new UnitOfWork())
            {
                _shopBasicInfoRepository.RemoveInfo(basic);
                _shopBasicInfoRepository.AddInfo(basic);
                if (shopSupports != null)
                {
                    _shopSupportsInfoReponsitory.RemoveInfoByShopid(basic.ShopId, unit);
                    foreach (var shopSupport in shopSupports)
                    {
                        shopSupport.ShopId = basic.ShopId;
                        _shopSupportsInfoReponsitory.AddInfo(shopSupport, unit);
                    }
                }
                if (appShopRelationShip != null)
                {
                    appShopRelationShip.ShopId = basic.ShopId;
                    _appShopRelationShipReponsitory.RemoveRelationShip(appShopRelationShip);
                    _appShopRelationShipReponsitory.AddRelationShip(appShopRelationShip);
                }

                unit.Complete();
            }

            var shop = _shopBasicInfoRepository.GetShopByid(basic.ShopId);
            var extraInfo = _shopExtraInfoReponsitory.GetShopExtraInfoById(basic.ShopId);
            var supports = _shopSupportsInfoReponsitory.GetShopSupportsInfoById(basic.ShopId);
            var app = _appShopRelationShipReponsitory.GetRelationShipByShopid(basic.ShopId);
            return ShopInfoDto.Convert(shop, extraInfo, supports.ToList(), app.AppId);
        }

        public void DeleteShop(long shopid)
        {
            using (var unit = new UnitOfWork())

            {
                _shopBasicInfoRepository.RemoveInfoByShopid(shopid, unit);
                _shopExtraInfoReponsitory.RemoveInfoByShopid(shopid, unit);
                _shopSupportsInfoReponsitory.RemoveInfoByShopid(shopid, unit);
                _appShopRelationShipReponsitory.RemoveRelationByShopid(shopid, unit);
                _shopGsRelationShipReponsitory.RemoveRelationByShopid(shopid, unit);
                unit.Complete();
            }
        }

        public void SaveShopGsRelationShip(IList<ShopGsRelationShipEntity> list)
        {
            if (!list.Any()) return;
            using (var unit = new UnitOfWork())
            {
                _shopGsRelationShipReponsitory.RemoveRelationByShopid(list.FirstOrDefault()?.ShopId ?? 0, unit);
                foreach (var entity in list)
                    _shopGsRelationShipReponsitory.AddRelationShip(entity, unit);

                unit.Complete();
            }
        }

        public IList<OfflineShopEntity> GetImportableShops(string zlcompanykey)
        {
            return _offlineShopReponsitory.GetImportableShop(zlcompanykey);
        }

        public void ImportOfflineShops(IList<OfflineShopEntity> shops, string zlcompanykey, long updater, string appid)
        {
            using (var unit = new UnitOfWork())
            {
                foreach (var shop in shops)
                {
                    var newShopId = SnowflakeIdCreater.Instance().GetNextId();

                    _offlineShopRelationshipReponsitory.DeleteRelationshipByOfflineShop(shop.Shopid, zlcompanykey,
                        unit);
                    _offlineShopRelationshipReponsitory.AddRelationship(new OfflineShopRelationshipEntity
                    {
                        Zlcompanykey = zlcompanykey,
                        OfflineShopid = shop.Shopid,
                        Updater = updater,
                        UpdatedTime = DateTime.Now.GetNowChinaTime(),
                        ShopId = newShopId
                    }, unit);
                    _shopBasicInfoRepository.AddInfo(new ShopBasicInfoEntity
                    {
                        ZlCompanyKey = zlcompanykey,
                        ShopId = newShopId,
                        Updater = updater,
                        UpdatedTime = DateTime.Now.GetNowChinaTime(),
                        ShopPhone = shop.ShopPhone,
                        IsSendCenter = false,
                        IsSinceCenter = false,
                        ShopInfos = string.Empty,
                        ShopAdCode = string.Empty,
                        ShopAvatar = string.Empty,
                        ShopBulletin = string.Empty,
                        ShopDeliveryPrice = 0,
                        ShopDescription = string.Empty,
                        ShopDetailAddress = shop.ShopAddress,
                        ShopGpsAddress = string.Empty,
                        ShopLocation = string.Empty,
                        ShopMinOrderMy = 0,
                        ShopName = shop.ShopName,
                        ShopScore = 0
                    }, unit);
                    _appShopRelationShipReponsitory.AddRelationShip(new AppShopRelationShipEntity()
                    {
                        Updater = updater,
                        UpdatedTime = DateTime.Now.GetNowChinaTime(),
                        AppId = appid,
                        ShopId = newShopId
                    }, unit);
                    unit.Complete();
                }
            }
        }
    }
}